import React, { Component } from 'react'
import { Input, Button } from 'antd';
const { TextArea } = Input;
export default class SetProgram extends Component {
  constructor(props) {
    super(props);
    this.state = {
      code: "",
      program: ""
    }
  }
  render() {
    return (
      <div style={{marginTop: "100px"}}>
        
        <div style={{marginTop: "20px"}}>
          <TextArea
            onChange={this.handelChangeCode}
            value={this.state.code} 
            rows={4}
            placeholder="粘贴纸带 栗子：[-1,1,1,0,1,1,1,1,0,-1,-1]"
          /> 
        </div>
        <div style={{marginTop: "20px"}}>
          <TextArea
            onChange={this.handelChangeProgram}
            value={this.state.program} 
            rows={8}
            placeholder="粘贴程序 栗子： [
              {status: 0, read: -1, insert: 1, action: 1, turn: 1},
              {status: 0, read: 1, insert: 0, action: 1, turn: 0},
              {status: 0, read: 1, insert: 0, action: 1, turn: 0},
              {status: 1, read: -1, insert: 0, action: 1, turn: 2},
              {status: 1, read: 1, insert: 0, action: 1, turn: 0},
              {status: 1, read: 1, insert: 0, action: 1, turn: 0},
            ]"
          />
        </div>
        <div style={{marginTop: "20px"}}>
          <Button onClick={() => {this.saveData()}}>保存</Button>
        </div>
      </div>
    )
  }
  handelChangeCode = e => {
    let data = e.target.value;
    this.setState({code: data});
  }
  handelChangeProgram = e => {
    let data = e.target.value;
    this.setState({program: data});
  }
  saveData = () => {
    try {
      let code = this.state.code;
      console.log(code);
      if (code) {
        code = JSON.parse(code);
        this.props.setCode(code);
      }

      
      let program = this.state.program;
      console.log(program);
      if (program) {
        program = JSON.parse(program);
        this.props.setProgram(program);
      }
    } catch (e) {
      console.log(e);
    }
  }
}
